package com.daohang361.imqq.pugin.encoder;

import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.encoder.InterfaceEncoder;
import com.daohang361.imqq.protocol.QQ2011;
import com.daohang361.imqq.tool.ByteArray;
import com.daohang361.imqq.tool.Hex2Bytes;
import com.daohang361.imqq.tool.MyByteSendArray;
import com.daohang361.imqq.tool.MyUtil;

/** 
 * @author naner E-mail:likun3344@163.com 
 * @create time：2011-6-9 下午08:51:35 
 */
public class Encoder02  implements InterfaceEncoder{
	@Override
	public byte[] encode(QQUser myqq, int c) {
		ByteArray myByte = doHead(myqq, "0002");
		myByte.saveThis();//保存此段作为第0段
		myByte.putHex("2A");
		myByte.putHex("7C 33 84 C8");//crowd index
		myByte.putHex("00 F2");//length
		myByte.putHex("00 02");//is img
		
		myByte.putHex("01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00");//final 
		myByte.putHex(MyUtil.Get4HexTime());//
		myByte.putHex("AD 70 FF 6F");//random
		myByte.putHex("00"); 
		myByte.putHex("00 00 00");// --》color bgr
		myByte.putHex("09");// --》font size
		myByte.putHex("00");// --》font style
		myByte.putHex("86 00");// --》GB final 
		myByte.putHex("00 06");// --》length 
		myByte.putHex("E5 AE 8B E4 BD 93");// -->宋体
		myByte.putHex("00 00"); 
		myByte.putHex("03"); //-->image mark
		myByte.putHex("00 C3");// -->length
		myByte.putHex("02"); 
		myByte.putHex("00 2A");// -->image name length
		myByte.putHex(
		"7B 31 46 35 39 46 35 41 30 2D 43 30 42 32 2D 39 "+
		"34 38 35 2D 31 46 44 31 2D 37 33 32 35 38 38 43 "+
		"39 43 41 31 41 7D 2E 6A 70 67 ");
		myByte.putHex("04"); 
		myByte.putHex("00 04");// --》length
		myByte.putByte(myqq.getAttr("token_data_2E"));// --> 05 22 27 00 2E  receive
		myByte.putHex("05"); 
		myByte.putHex("00 04"); 
		myByte.putHex("C2 0C 73 7C"); 
		myByte.putHex("06"); 
		myByte.putHex("00 04"); 
		myByte.putHex("00 00 1F 40"); 
		myByte.putHex("07"); 
		myByte.putHex("00 01"); 
		myByte.putHex("43"); 
		myByte.putHex("08"); 
		myByte.putHex("00 10"); 
		myByte.putByte(myqq.getAttr("imgKey"));
		myByte.putHex("09");
		myByte.putHex("00 01"); 
		myByte.putHex("01"); 
		myByte.putHex("14"); 
		myByte.putHex("00 04"); 
		myByte.putHex("00 00 00 00"); 
		myByte.putHex("FF"); 
		myByte.putHex("00 5C");
		myByte.putHex(
	    "15 36 20 39 32 6B 41 31 43 20 65 37 65 30 66 36"+ 
		"65 63 32 30 63 37 33 37 63 20 20 20 20 31 66 34"+ 
		"30");// taken_data_17
		myByte.putByte(myqq.getAttr("imgKey"));

		myByte.putHex(
		"7B 31 46 35 39 46 35 41 30 2D 43 30 42 32 2D 39 "+
		"34 38 35 2D 31 46 44 31 2D 37 33 32 35 38 38 43 "+
		"39 43 41 31 41 7D 2E 6A 70 67");// [token_data_17_imgname]
		
		myByte.putHex("41");
		
		myByte.saveThis();//保存此段作为第一段
		System.out.println("SessionKey:"+Hex2Bytes.bytesToHexString(myqq.getAttr("SessionKey")));
		myByte.teaThis(1, 1,myqq.getAttr("SessionKey"));//加密第一段到第一段
		doTail(myByte);
		return myByte.getByteList();//返回完整的package
	}
	
	private boolean ignore=false;
	@Override
	public byte[] encode(QQUser myqq, int c,Object...o) {
		System.out.println("使用默认编码器 命令为:"+MyUtil.intTo2Hex(c));
		ByteArray array=doHead(myqq,MyUtil.intTo2Hex(c));
		array.saveThis();//保存此段作为第0段
		array.putByte(myqq.getSend());
		array.saveThis();//保存此段作为第一段
		array.teaThis(1, 1,myqq.getAttr("SessionKey"));//加密第一段到第一段
		array=doTail(array);
		return array.getByteList();
	}

	public ByteArray doHead(QQUser myqq,String HexCommand)
	{
		ByteArray myByte = new MyByteSendArray();
		myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_HEAD);
		myByte.putInt(QQ2011.QQ_PROTOCOL_VERSION);
		myByte.putHex(HexCommand);
		myByte.putInt(myqq.getPakage());//包增长应该在接受到此包后
		myByte.putInt(myqq.getQq());
		myByte.putByte(QQ2011.QQ_CONTEX_FIX_12);
		return myByte;
	}
	public ByteArray doTail(ByteArray myByte)
	{
		myByte.putInt(QQ2011.QQ_PROTOCOL_PACKAGE_TAIL);
		myByte.saveThis();//保存此段作为第二段
		return myByte;//返回完整的package
	}
	
	@Override
	public boolean getIgnore() {
		
		return ignore;
	}
	
	@Override
	public void setIgnore(boolean ignore) {
		this.ignore=ignore;
	}
	@Override
	public Object invoke(Object... o) {
	return null;
	}
}
